package com.ruoyi.web.cp.schuedule;

import com.ruoyi.web.cp.service.*;
import com.ruoyi.web.cp.service.cp.Ao52Service;
import com.ruoyi.web.cp.service.cp.Ao53Service;
import com.ruoyi.web.cp.service.cp.Ao5Service;
import com.ruoyi.web.cp.service.ob.ObGameService;
import com.ruoyi.web.cp.service.ob.ObSxAllGoalService;
import com.ruoyi.web.cp.service.rm.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

import static com.ruoyi.web.constant.Constants.*;

@Slf4j
@Component
@ConditionalOnProperty(name = "ruoyi.quartz.enabled", havingValue = "true", matchIfMissing = true)
public class GameListTask {
    @Autowired
    private RmService rmService;
    @Autowired
    private ObService obService;
    @Autowired
    private FbService fbService;
    @Autowired
    private AqService aqService;
    @Autowired
    private RmResultService rmResultService;
    @Autowired
    private RmResultDoubleMergeService rmResultDoubleMergeService;
    @Autowired
    private RmResultDyMergeService rmResultDyMergeService;
    @Autowired
    private RmobResultMergeService rmobResultMergeService;
    @Autowired
    private RmResultChatShowMergeService rmResultChatShowMergeService;
    @Autowired
    private RmResultDx2MergeService rmResultDx2MergeService;
    @Autowired
    private RmobResult2MergeService rmobResult2MergeService;
    @Autowired
    private Ao5Service ao5Service;
    @Autowired
    private ObGameService obGameService;
    @Autowired
    private RmobGroupService rmobGroupService;
    @Autowired
    private RmobGroup2Service rmobGroup2Service;
    @Autowired
    private RmobGroup3Service rmobGroup3Service;
    @Autowired
    private Ao52Service ao52Service;
    @Autowired
    private ObSxAllGoalService obSxAllGoalService;
    @Autowired
    private Ao53Service ao53Service;
    @Autowired
    private WarnService warnService;
    @Autowired
    private SendFitService sendFitService;

    public void updateGameList() {
        log.info("======> updateGameList开始");
        try {
            rmService.getRmList();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        try {
            obService.getObList();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        try {
            fbService.getFbList();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        try {
            aqService.getList();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    /**
     * @Description: 更新rm详情
     * @Author: xiesm
     * @Date: 2025/3/25
     */
    @Scheduled(fixedDelayString = "3600000")
    public void updateRmDetail() {
        try {
            rmService.updateRmDetail();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    /**
     * @author xiesm
     * @description 更新rm结果
     * @date 2025/7/20
     */
    public void saveRmResult() {
        log.info("======> saveRmResult开始");
        try {
            rmResultService.saveRmResult();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        try {
            warnService.execute();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    public void savaRmobResult() {
        log.info("======> savaRmobResult开始");
        try {
            rmobResultMergeService.merge(RM_OB_MERGE, RM_OB_MERGE.contains("half"), "RMOB全场结果");
            rmobResultMergeService.merge(RM_OB_HALF_MERGE, RM_OB_HALF_MERGE.contains("half"), "RMOB半场结果");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    public void savaRmfbResult() {
        log.info("======> savaRmfbResult开始");
        try {
            rmobResultMergeService.merge(RM_FB_MERGE, RM_FB_MERGE.contains("half"), "RMFB全场结果");
            rmobResultMergeService.merge(RM_FB_HALF_MERGE, RM_FB_HALF_MERGE.contains("half"), "RMFB半场结果");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    public void savaRmobDoubleResult() {
        log.info("======> savaRmobDoubleResult开始");
        try {
            rmResultDoubleMergeService.merge(null, new Date());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    public void savaRmChatShow() {
        log.info("======> savaRmChatShow开始");
        try {
            rmResultChatShowMergeService.merge(null, null);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    public void savaRmobDyResult() {
        log.info("======> savaRmobDyResult开始");
        try {
            rmResultDyMergeService.merge(null, new Date());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    public void savaRmobDx2Result() {
        log.info("======> savaRmobDyResult开始");
        try {
            rmResultDx2MergeService.merge(null, new Date());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    public void sendRmobResultAll(Integer days, String name) {
        log.info("======> sendRmobResultAll开始");
        try {
            rmobResult2MergeService.merge(days, name);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    public void getAo5ListRecent2Day() {
        log.info("======> getAo5ListRecent2Day开始");
        try {
            ao5Service.getAo5ListRecent2Day();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    public void saveObGameResult() {
        log.info("======> saveObGameResult开始");
        try {
            obGameService.saveObGameResult();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    public void sendObGameResult(String days, String key) {
        log.info("======> sendObGameResult开始");
        try {
            obGameService.sendObGameResult(days, key);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    public void sendRMobGroup(Integer days, String key) {
        log.info("======> sendRMobGroup开始");
        try {
            rmobGroupService.execute(days, key);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    public void sendRMobGroup2(Integer days, String key) {
        log.info("======> sendRMobGroup2开始");
        try {
            rmobGroup2Service.execute(days, key);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    public void sendRMobGroup3(Integer days, String key) {
        log.info("======> sendRMobGroup3开始");
        try {
            rmobGroup3Service.execute(days, key);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    public void checkAo5_2() {
        log.info("======> checkAo5_2开始");
        try {
            ao52Service.checkAo5_2(null);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    public void obSxAllGoal(Integer days, String plv, String key) {
        log.info("======> checkAo5_2开始");
        try {
            double plvValue = 0.0;
            if (plv != null && !plv.isEmpty()) {
                plvValue = Double.parseDouble(plv);
            }
            obSxAllGoalService.execute(days, plvValue, key);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    public void ao5Yl(String key) {
        log.info("======> ao5Yl开始");
        try {
            ao53Service.execute(key);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    public void sendObFb150() {
        log.info("======> ao5Yl开始");
        try {
            sendFitService.sendRmobFbFit();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }


}
